package com.lsc.jiujia.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import java.net.HttpURLConnection;
import java.security.KeyStore;


@Configuration
@Slf4j
public class CommonConfig {

    private static final int READ_TIMEOUT = 5 * 1000;
    private static final int CONNECT_TIMEOUT = 5 * 1000;

    @Bean(name = "restTemplate")
    @Qualifier(value = "restTemplate")
    @Primary
    public RestTemplate restTemplate(){

        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(){
            @Override
            protected void prepareConnection(HttpURLConnection connection, String httpMethod) {
                try {
                    if (!(connection instanceof HttpsURLConnection)) {
                        super.prepareConnection(connection, httpMethod);
                    }
                    if (connection instanceof HttpsURLConnection) {
                        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
                        TrustStrategy anyTrustStrategy = (x509Certificates, str) -> true;
                        SSLContext ctx = SSLContexts.custom().loadTrustMaterial(trustStore, anyTrustStrategy).build();
                        ((HttpsURLConnection) connection).setSSLSocketFactory(ctx.getSocketFactory());
                        HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
                        httpsConnection.setHostnameVerifier(NoopHostnameVerifier.INSTANCE);
                        httpsConnection.setReadTimeout(READ_TIMEOUT);
                        httpsConnection.setConnectTimeout(CONNECT_TIMEOUT);
                        super.prepareConnection(httpsConnection, httpMethod);
                    }
                } catch (Exception e) {
                    log.info("prepareConnection->Exception:{}",e.getMessage());
                }
            }
        };
        factory.setOutputStreaming(false);
        return new RestTemplate(factory);
    }
}
